from selenium.common import NoSuchElementException, TimeoutException
from selenium.webdriver.common.by import By
from frp_page_object.base_page import BasePage, logger


class LoginPageOfc(BasePage):
    url = r'https://ofc-dev.kukasofa.com/pms'
    loc_name = (By.ID, 'username')
    loc_password = (By.ID, 'password')
    loc_login = (By.XPATH, '//*[@id="pms-app"]//button')
    loc_succeed = (By.CSS_SELECTOR, '.avatarDropdown')
    loc_failed = (By.XPATH, '//*[contains(text(),"知道了")]')

    def open(self):
        self.get(self.url)

    def input_name(self, name):
        self.input(self.loc_name, name)

    def input_password(self, password):
        self.input(self.loc_password, password)

    def click_login(self):
        self.click(self.loc_login)

    def login(self, name='Test1', password='Kuka123456'):
        self.input_name(name)
        self.input_password(password)
        self.click_login()

    # 检查是否登录成功
    def check_login(self):
        try:
            self.click(self.loc_failed, 3)
        except NoSuchElementException or TimeoutException:
            try:
                self.locator(self.loc_succeed, 8)
            except NoSuchElementException or TimeoutException:
                logger.error('ofc登录失败')
                return False
            else:
                return True
        else:
            return False
